package offer.offer02;

/**
 * 写两个子程序, 一个是把外面一圈写入, 一个是剥离出子矩阵
 */
public class Solution29 {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return new int[0];
        int[] result = new int[matrix.length * matrix[0].length];
        int resultLenLimit = result.length;
        int roundLimit = Math.min(matrix.length / 2, matrix[0].length / 2) + 1;
        System.out.println("可以剥离外围" + roundLimit + "次。");
        int resultCount = 0;
        int colNum = matrix[0].length;//列数量
        int rowNum = matrix.length; //行数量
        for(int i = 0; i < roundLimit; i ++){
            System.out.println("第" + i + "次剥离");
            //上面一行
            for(int j = i; j < colNum - i; j++){
                result[resultCount] = matrix[i][j];
                resultCount ++ ;
                if(resultCount == resultLenLimit) return result;
            }
            //右边一列
            for(int j = i + 1; j <rowNum - i - 1; j++){
                result[resultCount] = matrix[j][colNum - i - 1];
                resultCount++;
                if(resultCount == resultLenLimit) return result;
            }
            //下面一行
            for(int j = i; j < colNum - i; j++){
                result[resultCount] = matrix[rowNum - i - 1][colNum - j - 1];
                resultCount ++ ;
                if(resultCount == resultLenLimit) return result;
            }
            //左边一列
            for(int j = i + 1; j < rowNum - i - 1; j++){
                result[resultCount] = matrix[rowNum - j - 1][i];
                resultCount++;
                if(resultCount == resultLenLimit) return result;
            }
            System.out.println("第" + i + "次剥离的结果");
            for(int resultTmp : result) System.out.print(resultTmp + " ");
        }
        System.out.println("\n剥离过程结束: ");
        for(int resultTmp : result) System.out.print(resultTmp + " ");
        return result;
    }

    // 猛男落泪
    public int[] spiralOrder2(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.
            if(++t > b) break;
            for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.
            if(l > --r) break;
            for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.
            if(t > --b) break;
            for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.
            if(++l > r) break;
        }
        return res;
    }


    public static void main(String[] args) {
//        int[][] input = new int[][] {{11, 12, 13, 14, 15}, {21, 22, 23, 24, 25}, {31, 32, 33, 34, 35},
//                {41, 42, 43, 44, 45}, {51, 52, 53, 54, 55}, {61, 62, 63, 64, 65}};
//        int[][] input = new int[][] {{11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34},
//                {41, 42, 43, 44}, {51, 52, 53, 54}, {61, 62, 63, 64}};
//        int[][] input = new int[][] {{11, 12, 13, 14, 15, 16}, {21, 22, 23, 24, 25, 26}, {31, 32, 33, 34, 35, 36},
//                {41, 42, 43, 44, 45, 46}, {51, 52, 53, 54, 55, 56}, {61, 62, 63, 64, 65, 66}};
        int[][] input = new int[][] {{11, 12, 13, 14, 15}, {21, 22, 23, 24, 25}, {31, 32, 33, 34, 35},
                {41, 42, 43, 44, 45}, {51, 52, 53, 54, 55}};
        Solution29 solution29 = new Solution29();
        int[] resultGet = solution29.spiralOrder(input);
        System.out.println("最后结果:");
        for(int resultTmp : resultGet) System.out.print(resultTmp + " ");
    }
}
